export function successResponse(res, {
  code = 200,
  message = "请求成功！",
  data = {}
} = {}) {
  //res.writeHead(code, { "Content-Type": "application/json" });
  res.end(JSON.stringify({ code, status: 0, message, data }));
}

export function errorResponse(res, {
  code = 200,
  status = 1, 
  message = "请求失败！",
  data = {}
} = {}) {
  //res.writeHead(code, { "Content-Type": "application/json" });
  res.end(JSON.stringify({ code, status, message, data }));
}

 export function buildTree(list) {
    const map = new Map();
    const roots = [];
  
    // 先为每项创建 children 数组
    list.forEach((item) => {
      item.children = [];
      map.set(item.id, item);
    });
  
    list.forEach((item) => {
      if (item.parent_id && map.has(item.parent_id)) {
        map.get(item.parent_id).children.push(item);
      } else {
        roots.push(item);
      }
    });
  
    return roots;
  }